package com.czy.owner.ui.store;

import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.adapter.StoreMoreAdapter;
import com.czy.owner.api.OwnerStoreInfoApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CouponsCountModel;
import com.czy.owner.entity.StoreDetailInfoModel;
import com.czy.owner.entity.StoreMoreModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.business.BusinessScopeActivity;
import com.czy.owner.ui.business.ScopeBusinessActivity;
import com.czy.owner.ui.caseshow.CaseShowActivity;
import com.czy.owner.ui.message.ContactsActivity;
import com.czy.owner.ui.userinfo.FeedBackActivity;
import com.czy.owner.utils.GsonUtils;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.easyrecycleview.decoration.HorizontalDividerDecoration;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by asus on 2017/5/18.
 */
public class StoreMoreActivity extends BaseActivity {


    @BindView(R.id.rv_store_more)
    RecyclerView rvStoreMore;
    private List<StoreMoreModel> storeList = new ArrayList<>();
    private StoreDetailInfoModel.StoreInfoBean infoModel;//门店详情数据

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_store_more;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.more);
        getStoreInfoData();
        String[] serviceNames = getResources().getStringArray(R.array.store_more_names);
        int[] serviceImgs = new int[]{R.mipmap.icon_store_casee,R.mipmap.icon_store_shop, R.mipmap.icon_store_operation,
                R.mipmap.icon_store_act, R.mipmap.icon_store_book, R.mipmap.icon_store_wifi,
                R.mipmap.icon_store_gps, R.mipmap.icon_store_phone, R.mipmap.icon_store_feedbac,
                R.mipmap.transparent_bg,R.mipmap.transparent_bg,R.mipmap.transparent_bg};
        for (int i = 0; i < serviceNames.length; i++) {
            StoreMoreModel model = new StoreMoreModel();
            model.setImg(serviceImgs[i]);
            model.setName(serviceNames[i]);
            storeList.add(model);
        }
        rvStoreMore.setLayoutManager(new GridLayoutManager(this, 4));
        rvStoreMore.addItemDecoration(new HorizontalDividerDecoration(this));
        StoreMoreAdapter adapter = new StoreMoreAdapter(this);
        rvStoreMore.setAdapter(adapter);
        adapter.addAll(storeList);
//        wangyang新增
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Object o) {
                StoreMoreModel model = (StoreMoreModel) o;
               if(model.getName().equals("案例")){
                   startActivity(new Intent(StoreMoreActivity.this, CaseShowActivity.class));
               }else if (model.getName().equals("门店简介")) {
                    startActivity(new Intent(StoreMoreActivity.this, BindStoreDetailActivity.class));
                } else if (model.getName().equals("业务范围")) {
//                    startActivity(new Intent(StoreMoreActivity.this, ScopeBusinessActivity.class));
                    startActivity(new Intent(StoreMoreActivity.this, BusinessScopeActivity.class));
                } else if (model.getName().equals("门店活动")) {
                    startActivity(new Intent(StoreMoreActivity.this, StoreActivity.class));
                } else if (model.getName().equals("通讯录")) {
                    startActivity(new Intent(StoreMoreActivity.this, ContactsActivity.class));
                } else if (model.getName().equals("WIFI密码")) {
                    StoreUtils.WIFIDialog(StoreMoreActivity.this, "嘻嘻!被你发现了,尽情享用吧!", "账号一" + infoModel.getWifiName() + "\n密码一" + infoModel.getWifiPass(), "好的");
                } else if (model.getName().equals("导航去店")) {
                    if ((Build.VERSION.SDK_INT >= 23)) {
                        StoreUtils.onClickLocation(StoreMoreActivity.this, infoModel);
                    } else {
                        StoreUtils.imgNavigation(StoreMoreActivity.this, infoModel);
                    }
                } else if (model.getName().equals("一键拨号")) {
                    if ((Build.VERSION.SDK_INT >= 23)) {
                        StoreUtils.onClickCallPhone(StoreMoreActivity.this, infoModel.getServiceNumber());
                    } else {
                        StoreUtils.PhoneDialog(StoreMoreActivity.this, "门店客服:" + infoModel.getServiceNumber(), "", "呼叫", "取消");
                    }
                } else if (model.getName().equals("门店反馈")) {
                    Intent intent = new Intent(StoreMoreActivity.this, FeedBackActivity.class);
                    intent.putExtra("feedback", "store");
                    startActivity(intent);
                }
            }
        });

    }

    @Override
    protected void updateViews() {

    }

    //wangyang新增
    private void getStoreInfoData() {
        OwnerStoreInfoApi ownerStoreInfoApi = new OwnerStoreInfoApi(new HttpOnNextListener<StoreDetailInfoModel>() {
            @Override
            public void onNext(StoreDetailInfoModel model) {
                infoModel = model.getStoreInfo();
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<StoreDetailInfoModel>>() {
                }.getType();
                BaseResultEntity<StoreDetailInfoModel> model = gson.fromJson(cache, type);
                infoModel = model.getData().getStoreInfo();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(StoreMoreActivity.this, e.getMessage());
            }

        }, this);
        ownerStoreInfoApi.setStoreId(SharedPreferencesUtils.getParam(StoreMoreActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(ownerStoreInfoApi);
    }


}
